/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Review.test3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author THIEN
 */
public class StudentTest {

    Student student;
    ArrayList<Student> StudentList = new ArrayList<Student>();
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);

    public void menu() throws IOException {
        int choice;
        do {
            System.out.println("\t1. Add");
            System.out.println("\t2. View");
            System.out.println("\t3. Save");
            System.out.println("\t4. Read");
            System.out.println("\t5. Search nguyen or NGuyeN");
            System.out.println("\t6. Exit");
            System.out.println("Enter your choice:");
            choice = Integer.parseInt(in.readLine());
            switch (choice) {
                case 1:
                    this.inputData();
                    break;
                case 2:
                    this.displayData();
                    break;
                case 3:
                    this.save2file();
                    break;
                case 4:
                    this.readFromFile();
                    break;
                case 5:
                    this.search();
                    break;
                case 6:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid");

            }
        } while (choice != 6);
    }

    public void inputData() throws IOException {
        for (int i = 0; i < 2; i++) {
            System.out.println("Roll Number: ");
            String rollNumber = in.readLine();

            System.out.println("Name: ");
            String name = in.readLine();

            System.out.println("Address: ");
            String address = in.readLine();

            student = new Student(rollNumber, name, address);
            StudentList.add(student);

        }

    }

    public void displayData() {
        for (Student student : StudentList) {
            System.out.println(student);
        }

    }

    public void save2file() throws FileNotFoundException, IOException {
        FileOutputStream f = new FileOutputStream("stu2.txt");
        ObjectOutputStream writer = new ObjectOutputStream(f);
        writer.writeObject(StudentList);
        writer.flush();
        writer.close();
        f.close();
    }

    public void readFromFile() throws FileNotFoundException {
        FileReader fReader = new FileReader("stu2.txt");
        BufferedReader bReader = new BufferedReader(fReader);
        for (Student student : StudentList) {
            System.out.println(student);

        }
    }

    public void search() {
        String pattStr = "[nN][gG]uye[nN]";
        Pattern pattObj = Pattern.compile(pattStr);
        for (Student student : StudentList) {
            String temp = student.getName();
            Matcher matObj = pattObj.matcher(temp);
            while (matObj.find()) {
                System.out.println(student);
            }

        }
    }

    public static void main(String args[]) throws IOException {
        StudentTest ST = new StudentTest();
        ST.menu();
    }
}
